package tw.rescue.controller.slim3.message;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.xwork.StringUtils;
import org.slim3.controller.Navigation;
import tw.rescue.biz.MessageBoardBiz;
import tw.rescue.model.slim3.model.Message;

public class MessageBoardController extends HouseKeepingController {

	private MessageBoardBiz messageBoardBiz;
	
	public void setMessageBoardBiz(MessageBoardBiz messageBoardBiz) {
		this.messageBoardBiz = messageBoardBiz;
	}

	@Override
	protected Navigation run() throws Exception {
		String action = asString("action");
		if (StringUtils.isBlank(action))
			action = "list";
		else
			action = action.trim();
		Navigation navigation = null;
		if ("list".equals(action))
			navigation = doList();
		else if ("new".equals(action))
			navigation = doNew();
		else if ("reply".equals(action))
			navigation = doReply();
		else if ("view".equals(action))
			navigation = doView();
		else if ("delete".equals(action))
			navigation = doDelete();
		else if ("prosecute".equals(action))
			navigation = doProsecute();
		return navigation;
	}

	private Navigation doList() throws Exception {
		Integer from = asInteger("from");
		Integer count = asInteger("count");
		if (from==null)
			from = 0;
		if (count==null)
			count = 20;
		List<Message>list = messageBoardBiz.getRecentMessages(asAccount(), asCategory(), from, count+1);
		request.setAttribute("previous", Math.max(0, from-count));
		request.setAttribute("from", from);
		request.setAttribute("count", count);
		request.setAttribute("hasNextPage", list.size()>count);
		request.setAttribute("messageList", list.subList(0, Math.min(list.size(), count)));
		return forward("/WEB-INF/jsp/pages/message/page-message_list.jsp");
	}

	protected Navigation doNew() throws Exception {
		Message msg = new Message();
		msg.setNick(asString("nick"));
		msg.setEmail(asString("email"));
		msg.setSubject(asString("subject"));
		msg.setContent(asString("content"));
		Set<String>categories = new HashSet<String>();
		categories.add("test");
		msg.setCategories(categories);
		messageBoardBiz.newMessage(asAccount(), msg);
		return redirect("/slim3/message/messageBoard?action=list");
	}
	
	private Navigation doReply() throws Exception {
		Message msg = new Message();
		msg.setNick(asString("nick"));
		msg.setEmail(asString("email"));
		msg.setSubject(asString("subject"));
		msg.setContent(asString("content"));
		Set<String>categories = new HashSet<String>();
		categories.add("test");
		msg.setCategories(categories);
		messageBoardBiz.replyMessage(asAccount(), asString("replyTo"), msg);
		return redirect("/slim3/message/messageBoard?action=list");
	}

	
	private Navigation doView() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	private Navigation doDelete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	private Navigation doProsecute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
